package model;
/**
 * @author Darko Martinovikj
 * @version 1.0 01.03.2013
 * 
 * Class representing a hash table for storing all the variables with their current values and fast retrieving 
 *  */
import java.util.HashMap;
import java.util.Map;

/** Class represening the hash table for storing all the variables and their values
 * @author DarkoLaptop2
 *
 */
public class VariableHashTable {

	private HashMap<String,Integer> table;
	
	/**
	 * Constructor of the class. Creates a new hash table
	 *
	 */
	public VariableHashTable() {
		table=new HashMap<String,Integer>();
	}
	
	/**
	 * Method for variable insertion. The variable name and value must be specified
	 *
	 *@param String varName - name of the variable
	 *@param int value - value of the variable
	 */
	public void insertVariable(String varName,int value){
		table.put(varName, value);
	}
	
	/**
	 * Method for variable insertion. The variable name must be specified. The variable value is set to 0.
	 *
	 *@param String varName - name of the variable
	 */
	public void insertVariable(String varName){
		table.put(varName, null);
	}
	
	/**
	 * Method for variable deletion. The variable name must be specified
	 *
	 *@param String varName - name of the variable which will be deleted
	 */
	public void removeVariable(String varName){
		table.remove(varName);
	}
	
	/**
	 * Method for variable value update. The variable name and new value must be specified
	 *
	 *@param String varName - name of the variable
	 *@param int newValue - new value of the variable
	 */
	public void updateVariableValue(String varName,int newValue){
		table.put(varName, newValue);
	}
	
	/**
	 * Method for returning all of the variables as Array of string
	 *
	 *@return String[] - Arrays of string, representing variables name
	 */
	public String[] getAllVariables(){
		String[] allVars=new String[table.keySet().size()];
		int i=0;
		for(String element:table.keySet())
			allVars[i++]=element;
		return allVars;
	}
	
	/**
	 * Method for getting the variable Value. If a variable with that name doesn't exist the method returns null
	 *
	 *@return Integer - Variable value or null
	 */
	public Integer getVariableValue(String varName){
		return table.get(varName);
	}
	
	/**
	 * toString method for the variable hashTable
	 * 
	 * @return String - The string containing all variables' information
	 */
	public String toString()
	{
		String s="Vars:";
		for(String element:table.keySet())
			s+=element+"="+getVariableValue(element)+", ";
		return s.substring(0,s.length()-2);
	}
	
	/**
	 * method for checking if two variable hash tables are equal
	 * They are equal if they contain the same variables and have the same values
	 * */
	@Override
	public boolean equals(Object o2){
		VariableHashTable table2=(VariableHashTable)o2;
		for(Map.Entry<String, Integer> entry : table.entrySet()){
			if(!table2.table.containsKey(entry.getKey()))
				return false;
			if(!table2.table.get(entry.getKey()).equals(entry.getValue()))
				return false;
		}
		return true;
	}
	
	/** Method for copying the variable hash table into a new hash table
	 * @return a copy of the variable hash table
	 */
	public VariableHashTable copyHashTable(){
		VariableHashTable variablesTable=new VariableHashTable();
		for(String variable:this.getAllVariables()){
			variablesTable.insertVariable(variable,this.getVariableValue(variable));
		}
		return variablesTable;
	}
}
